package com.zhou.Controller;

import com.zhou.domain.Rusult;
import com.zhou.domain.Msg;
import com.zhou.domain.UserInfoHolder;
import com.zhou.domain.msjData;
import com.zhou.server.impl.MessageRankService;
import com.zhou.server.impl.MsgServerImpl;
import com.zhou.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "消息相关接口")
public class MsgController {

    public static HashMap<Long, String> map = new HashMap<>();

    @Autowired
    private MsgServerImpl msgServer;

    public static String name;

    @Autowired
    private MessageRankService messageRankService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //查询全部消息
    @ApiOperation("查询全部消息")
    @GetMapping("/getall/{name}/{hyname}")
    public Rusult getMsgAll(@PathVariable(value = "name") String name, @PathVariable(value = "hyname") String hyname){
        List<msjData> list = msgServer.getall(name,hyname);
        return Rusult.success(list);
    }

    //插入消息接口
    @PostMapping("/addmsg")
    @ApiOperation("插入消息接口")
    public Rusult addmsg(@RequestBody Msg deserialize) throws Exception {

        //msgServer.addmsg(deserialize);
        name=deserialize.getName();

        redisTemplate.convertAndSend("redis.msg", JsonUtil.serialize(deserialize));

        messageRankService.incrementMessageCount(deserialize.getName());
        return Rusult.success();
    }

    //获取用户发言排名
    @GetMapping("/getranking")
    @ApiOperation("返回排名")
    public Rusult getranking() throws Exception {
        return Rusult.success(messageRankService.getTopUsers());
    }

}
